﻿using Hangfire;
using Hangfire.MySql;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Reflection;
using X23.ApplicationJob.Application;
using System.Transactions;
using Microsoft.AspNetCore.Builder;
using MySqlConnector;
using System.Data;

namespace X23.ApplicationJob
{
    public static class StartupJob
    {
        public static void AddX23Hangfire(this IServiceCollection services, string mysqlConnectionString)
        {
            //使用Dapper
            services.AddScoped(x => { return new MySqlConnection(mysqlConnectionString) as IDbConnection; });

            services.AddTransient<IJobService, JobService>();
            StaticDic.IniJobStaticDic(Assembly.Load("X23.Application"));

            services.AddHangfire(x =>
            {
                x.UseStorage(new MySqlStorage(
                    mysqlConnectionString,
                    new MySqlStorageOptions
                    {
                        TransactionIsolationLevel = System.Transactions.IsolationLevel.ReadCommitted, // 事务隔离级别。默认是读取已提交。
                        QueuePollInterval = TimeSpan.FromSeconds(15),             // 作业队列轮询间隔。默认值为15秒。
                        JobExpirationCheckInterval = TimeSpan.FromHours(1),       // 作业到期检查间隔（管理过期记录）。默认值为1小时。
                        CountersAggregateInterval = TimeSpan.FromMinutes(5),      // 聚合计数器的间隔。默认为5分钟。
                        PrepareSchemaIfNecessary = true,                          // 如果设置为true，则创建数据库表。默认是true。
                        DashboardJobListLimit = 50000,                            // 仪表板作业列表限制。默认值为50000。
                        TransactionTimeout = TimeSpan.FromMinutes(1),             // 交易超时。默认为1分钟。
                        //TablesPrefix = "X23"                                      // 数据库中表的前缀。默认为none
                    }
                    ));
            });
            services.AddHangfireServer();
        }

        public static void UseX23Hangfire(this IApplicationBuilder app)
        {
            app.UseHangfireDashboard();
            //AppPath = "/";
            //PrefixPath = string.Empty;
            //_asyncAuthorization = new IDashboardAsyncAuthorizationFilter[0];
            //Authorization = DefaultAuthorization;
            //IsReadOnlyFunc = ((DashboardContext _) => false);
            //StatsPollingInterval = 2000;
            //DisplayStorageConnectionString = true;
            //DashboardTitle = "Hangfire Dashboard";
            //DisplayNameFunc = null;
            //app.UseHangfireDashboard("/hangfire", new DashboardOptions
            //{
            //    Authorization = new[]
            //      {
            //         new BasicAuthAuthorizationFilter(new BasicAuthAuthorizationFilterOptions
            //        {
            //            SslRedirect = false,          // 是否将所有非SSL请求重定向到SSL URL
            //            RequireSsl = false,           // 需要SSL连接才能访问HangFire Dahsboard。强烈建议在使用基本身份验证时使用SSL
            //            LoginCaseSensitive = false,   //登录检查是否区分大小写
            //            Users = new[]                 //配置登陆账号和密码
            //            {
            //                new BasicAuthAuthorizationUser
            //                {
            //                    Login ="admin",//用户名
            //                    PasswordClear="123456"
            //                }
            //            }
            //        })
            //     }
            //});
        }
    }
}
